/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.api.wlhy.vehicle;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.EntityDict;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.enums.ExamineStatusEnum;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.DictUtil;
import org.jeecg.common.util.NumberValidationUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyDriverVehicle;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicleReq;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicleType;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyDriverVehicleService;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 在这里编写类的功能描述
 *
 * @author duanlikao
 * @created 2020/12/4
 */
@Api(tags = "APP车辆信息")
@RestController
@RequestMapping("/api/wlhy/vehicle")
@Slf4j
public class ApiVehicleController extends JeecgController<HyVehicle, IHyVehicleService> {

    @Autowired
    private IHyVehicleService hyVehicleService;

    @Autowired
    private IHyDriverVehicleService hyDriverVehicleService;

    @Resource
    private BaseCommonService baseCommonService;

    @Autowired
    private IHyDriverService hyDriverService;

    @Resource
    private IHySettingService settingService;


    @Autowired
    private DictUtil dictUtil;

    @Autowired
    private UserUtil userUtil;


    private String PLATENUMMATCH = "^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏黔川滇宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏黔川滇宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领临超]))$";


    /**
     * APP车辆信息-保存
     *
     * @param hyVehicle
     * @return
     */
    @AutoLog(value = "APP车辆信息-保存")
    @ApiOperation(value = "APP车辆信息-保存", notes = "APP车辆信息-保存")
    @PostMapping(value = "/save")
    public Result<?> save(@RequestBody @Valid HyVehicleReq hyVehicle) {

        HyDriver hyDriver = userUtil.getDriver();
        hyVehicle.setDriverId(hyDriver.getId());

        String feeCalc = HyCfgUtil.getCfgValue2("driver_upload_simplification");
        //修改车牌颜色为蓝色时校验运输证号bug
        if (StringUtils.isNotEmpty(hyVehicle.getVehiclePlateColorCode())&&hyVehicle.getVehiclePlateColorCode().equals("1")){
            feeCalc="1";
        }
        log.info("简化版参数打印： {}", feeCalc);
        if (StrUtil.isEmpty(feeCalc) || !feeCalc.equals("1")) {
            checkVehicle(hyVehicle);
        }

        //根据车牌号查询车辆
//        HyVehicle nowVehicle = hyVehicleService.getHyVehicleByVehicleNumber(hyVehicle.getVehicleNumber());
//        //车辆为空时 或者 车辆所属司机为本人时 要校验车辆数据 否则不校验
//        if (nowVehicle == null ||
//                (nowVehicle != null && StringUtils.equals(hyDriver.getId(), nowVehicle.getDriverId()))
//        ) {
//            checkVehicle(hyVehicle);
//        }

        //获取自动审核时间段
        String driverExamineTime = settingService.getSettingValueByTag("driver_examine_time");

        if (StringUtils.isNotEmpty(driverExamineTime)) {

            String[] split = driverExamineTime.split("-");
            boolean inDate = isInDate(new Date(), split, "HH:mm:ss", "Asia/Shanghai");
            if (inDate) {
                hyVehicle.setExamineStatus(ExamineStatusEnum.EXAMINE_STATUS_APPROVED.getCode());
            } else {
                hyVehicle.setExamineStatus(ExamineStatusEnum.EXAMINE_STATUS_TOREVIEWED.getCode());
            }
        } else {
            hyVehicle.setExamineStatus(ExamineStatusEnum.EXAMINE_STATUS_APPROVED.getCode());
        }


        Result<JSONObject> result = hyVehicleService.saveHyVehicle(HyVehicleReqTOHyVehicle(hyVehicle));
        if (!result.isSuccess()) {
            return result;
        }

        //添加日志
        baseCommonService.addLog("车辆信息: " + hyVehicle.getVehicleNumber() + ",保存成功！",
                CommonConstant.LOG_TYPE_1, null);
        return result;
    }

    @AutoLog(value = "APP车辆信息-车辆更新")
    @ApiOperation(value = "APP车辆信息-车辆更新", notes = "APP车辆信息-车辆更新")
    @PostMapping(value = "/upVehicle")
    @CacheEvict(value = CacheConstant.SYS_DICT_TABLE_CACHE ,key = "'SimpleKey [hy_vehicle,vehicle_number,id,' + #hyVehicle.id + ']'")
    public Result<?> upVehicle(@RequestBody @Valid HyVehicleReq hyVehicle) {
//        if(StringUtils.isBlank(hyVehicle.getId())){
//            return Result.error("车辆ID不能为空");
//        }

        HyDriver hyDriver = userUtil.getDriver();
        //根据车牌号查询车辆
        HyVehicle nowVehicle = hyVehicleService.getById(hyVehicle.getId());
        if (nowVehicle == null) {
            return Result.error("车辆不存在");
        }
//        if (!StringUtils.equals(hyDriver.getId(), hyVehicle.getDriverId())) {
//            return Result.error("不是本人车辆,不能编辑");
//        }

        if (!StringUtils.equals(nowVehicle.getVehicleNumber(), hyVehicle.getVehicleNumber())) {
            HyVehicle vehicleDb = hyVehicleService.getHyVehicleByVehicleNumber(hyVehicle.getVehicleNumber());
            if (vehicleDb != null) {
                return Result.error("车牌号已存在");
            }
        }

        String feeCalc = HyCfgUtil.getCfgValue2("driver_upload_simplification");
        //修改车牌颜色为蓝色时校验运输证号bug
        if (hyVehicle.getVehiclePlateColorCode().equals("1")){
            feeCalc="1";
        }
        if (StrUtil.isEmpty(feeCalc) || !feeCalc.equals("1")) {
            checkVehicle(hyVehicle);
        }

        hyVehicle.setDriverId(hyDriver.getId());
        HyVehicle hyVehicle1 = HyVehicleReqTOHyVehicle(hyVehicle);
        hyVehicle1.setId(nowVehicle.getId());
        hyVehicleService.updateById(hyVehicle1);
        hyVehicleService.binding(hyDriver.getId(),  hyVehicle1.getId()) ;
        //添加日志
        baseCommonService.addLog("车辆信息: " + hyVehicle.getVehicleNumber() + ",编辑保存成功！",
                CommonConstant.LOG_TYPE_1, null);
        return Result.OK();
    }

    private void checkVehicle(@RequestBody @Valid HyVehicleReq hyVehicle) {

        if (StringUtils.isNotEmpty(hyVehicle.getVehicleNumber())) {
            if (!hyVehicle.getVehicleNumber().matches(PLATENUMMATCH)) {
                throw new JeecgBootException("车牌照号不正确,请输入正确的车牌号,英文字母大写!");
            }
        }

        if (StringUtils.isEmpty(hyVehicle.getRoadTransportNumber())) {
            throw new JeecgBootException("道路运输证号不允许为空");
        }

        if (StringUtils.isEmpty(hyVehicle.getRegisterDate())) {
            throw new JeecgBootException("注册日期不允许为空");
        }
        Date registerDate = DateUtils.getStrToDate(hyVehicle.getRegisterDate());
        if (registerDate == null) {
            throw new JeecgBootException("注册日期格式错误,请重新填写!");
        }

        if (registerDate.after(new Date())) {
            throw new JeecgBootException("注册日期过去的时间,请重新填写!");
        }

        if (StringUtils.isEmpty(hyVehicle.getIssueDate())) {
            throw new JeecgBootException("发证日期不允许为空");
        }
        Date issueDate = DateUtils.getStrToDate(hyVehicle.getIssueDate());
        if (issueDate == null) {
            throw new JeecgBootException("发证日期格式错误,请重新填写!");
        }

        if (issueDate.after(new Date())) {
            throw new JeecgBootException("发证日期需要一个过去的时间,请重新填写!");
        }
        if (StringUtils.isNotEmpty(hyVehicle.getVehicleTonnage()) &&
                !NumberValidationUtil.isNonNegativeRealNumber(hyVehicle.getVehicleTonnage())) {
            hyVehicle.setVehicleTonnage("");
            //throw new JeecgBootException("核定载质量格式不正确,请重新填写!例:30.00 ");
        }
        if (StringUtils.isNotEmpty(hyVehicle.getGrossMass()) &&
                !NumberValidationUtil.isNonNegativeRealNumber(hyVehicle.getGrossMass())) {
            hyVehicle.setGrossMass("");
            //throw new JeecgBootException("吨位/车辆总质量格式不正确,请重新填写!例:30.00 ");
        }

        if (StringUtils.isNotEmpty(hyVehicle.getDrivingLicenseNum()) &&
                !NumberValidationUtil.isNumeric(hyVehicle.getDrivingLicenseNum())) {
            hyVehicle.setDrivingLicenseNum("");
            //throw new JeecgBootException("行驶证档案编号格式不正确,请重新填写!");
        }
        if (StringUtils.isNotEmpty(hyVehicle.getVehiclePlateColorCode()) &&
                !NumberValidationUtil.isNumeric(hyVehicle.getVehiclePlateColorCode())) {
            throw new JeecgBootException("车牌颜色不正确,请重新选择!");
        }
        if (StringUtils.isNotEmpty(hyVehicle.getVehicleLong()) &&
                !NumberValidationUtil.isNumeric(hyVehicle.getVehicleLong())) {
            hyVehicle.setVehicleLong("");
            //throw new JeecgBootException("车辆长度格式不正确,请重新填写!");
        }
        if (StringUtils.isNotEmpty(hyVehicle.getVehicleWidth()) &&
                !NumberValidationUtil.isNumeric(hyVehicle.getVehicleWidth())) {
            hyVehicle.setVehicleWidth("");
            //throw new JeecgBootException("车辆宽度格式不正确,请重新填写!");
        }
        if (StringUtils.isNotEmpty(hyVehicle.getVehicleHeight()) &&
                !NumberValidationUtil.isNumeric(hyVehicle.getVehicleHeight())) {
            hyVehicle.setVehicleHeight("");
            throw new JeecgBootException("车辆高度格式不正确,请重新填写!");
        }


        HyVehicleType hyVehicleType = hyVehicleService.getHyVehicleType(hyVehicle.getVehicleType());
        if (hyVehicleType == null) {
            throw new JeecgBootException("车辆类型不正确请重新填写！");
        }
        if (StringUtils.isEmpty(hyVehicle.getBrandName())) {
            throw new JeecgBootException("请填写车辆品牌型号!");
        }

        if (StringUtils.isEmpty(hyVehicle.getVehicleEnergyType())) {
            throw new JeecgBootException("请填写车辆能源类型！");
        }

        //挂车行驶证检测,挂车行驶证开关如果关闭，注册添加车辆时不验证挂车
        if ("1".equals(settingService.getSettingValueByTag2("trailerVehiclImg_upload"))) {
            //挂车信息检测
            if (hyVehicleType.getText().contains("挂")) {
                if (StringUtils.isEmpty(hyVehicle.getTrailerVehiclLicenseImg())) {
                    throw new JeecgBootException("请上传挂车行驶证(正面)照片");
                }

                if (StringUtils.isEmpty(hyVehicle.getTrailerVehiclLicenseImg2())) {
                    throw new JeecgBootException("请上传挂车行驶证(副页)照片");
                }

                if (StringUtils.isEmpty(hyVehicle.getTrailerVehicleRoadTransportImg())) {
                    throw new JeecgBootException("请上传挂车道路运输证照片");
                }

                if (StrUtil.isEmpty(hyVehicle.getTrailerVehicleType())) {
                    throw new JeecgBootException("挂车车辆类型不能为空");
                }

                if (StrUtil.isEmpty(hyVehicle.getTrailerVehicleRoadTransportNumber())) {
                    throw new JeecgBootException("挂车道路运输证号不能为空");
                }

                if (hyVehicle.getTrailerVehicleRoadTransportNumber().length() < 6) {
                    throw new JeecgBootException("挂车道路运输证号不正确");
                }

                if (StringUtils.isEmpty(hyVehicle.getTrailerVehiclePlateNumber())) {
                    throw new JeecgBootException("请填写挂车牌照号!");
                } else {
                    if (!hyVehicle.getTrailerVehiclePlateNumber().endsWith("挂") &&
                            !hyVehicle.getTrailerVehiclePlateNumber().endsWith("超")) {
                        throw new JeecgBootException("挂车牌照号不正确!");
                    }

                    if (!hyVehicle.getTrailerVehiclePlateNumber().matches(PLATENUMMATCH)) {
                        throw new JeecgBootException("挂车牌照号不正确,请输入正确的车牌号,英文字母大写!");
                    }

                }
                if (StringUtils.isEmpty(hyVehicle.getTrailerVehiclePlateColorCode())) {
                    hyVehicle.setTrailerVehiclePlateColorCode(hyVehicle.getVehiclePlateColorCode());
                }
            }
        }

        //}

        //辽宁税务需要 start
//        if(StringUtils.isNotEmpty(hyVehicle.getRegistCertNo()) &&
//                !NumberValidationUtil.isNumeric(hyVehicle.getRoadTransportNumber()) ){
//            throw new JeecgBootException("机动车登记证书编号格式不正确,请重新填写!");
//        }
        //辽宁税务需要 end

        //快货运需要 start
        String zhuhai_check = settingService.getSettingValueByTag2("zhuhai_check");
        if (zhuhai_check.equals("1")) {
            //挂车信息检测
            if (hyVehicleType.getText().contains("挂")) {
                if (StringUtils.isEmpty(hyVehicle.getTrailerVehiclePlateNumber())) {
                    throw new JeecgBootException("请填写挂车牌照号!");
                } else {
                    if (!hyVehicle.getTrailerVehiclePlateNumber().endsWith("挂") &&
                            !hyVehicle.getTrailerVehiclePlateNumber().endsWith("超")) {
                        throw new JeecgBootException("挂车牌照号不正确!");
                    }
                    if (!hyVehicle.getTrailerVehiclePlateNumber().matches(PLATENUMMATCH)) {
                        throw new JeecgBootException("挂车牌照号不正确,请输入正确的车牌号,英文字母大写!");
                    }
                }
                if (StringUtils.isEmpty(hyVehicle.getTrailerVehiclePlateColorCode())) {
                    hyVehicle.setTrailerVehiclePlateColorCode(hyVehicle.getVehiclePlateColorCode());
                }
            }

            //检查是否开启道路运输证业户名称
            if (StringUtils.isEmpty(hyVehicle.getRoadTransportName())) {
                throw new JeecgBootException("请填写道路运输证业户名称!");
            }
            //检查是否开启路运输证过期时间
            if (StringUtils.isNotEmpty(hyVehicle.getRoadTransportExpiredDate())) {
                Date roadTransportExpiredDate = DateUtils.getStrToDate(hyVehicle.getRoadTransportExpiredDate());
                if (roadTransportExpiredDate == null) {
                    throw new JeecgBootException("道路运输证过期时间格式错误,请重新填写!");
                }
            }
            //车籍地
            if (StringUtils.isEmpty(hyVehicle.getVehicleAddress())) {
                throw new JeecgBootException("请填写车籍地（行驶证住址）!");
            }
        }
        //快货运需要 end


        //车辆排放标准检测
        String vehicle_emission_settings = settingService.getSettingValueByTag2("vehicle_emission_settings");
        if ("1".equals(vehicle_emission_settings)) {

            if (StringUtils.isBlank(hyVehicle.getEmissionStandardType())) {
                throw new JeecgBootException("请选择车辆排放标准!");
            }
            if (StringUtils.isBlank(hyVehicle.getEnvironmentalCertificate())) {
                throw new JeecgBootException("请上传环保证书!");
            }
        }

        //车辆为蓝牌车辆的不需要上传道路运输证
        if(!"1".equals(hyVehicle.getVehiclePlateColorCode())){
            //道路运输证检测
            if ("1".equals(settingService.getSettingValueByTag2("roadTransportImg_upload"))) {

                if (StringUtils.isEmpty(hyVehicle.getRoadTransportImg())) {
                    throw new JeecgBootException("请上传车辆道路运输证照片!");
                }
            }
            if (StringUtils.isNotEmpty(hyVehicle.getRoadTransportNumber()) &&
                    !NumberValidationUtil.isNumeric(hyVehicle.getRoadTransportNumber())) {
                throw new JeecgBootException("车辆道路运输证号格式不正确,请重新填写!");
            }
        }



        //车辆照片(人和车和车牌)照片检测
        if ("1".equals(settingService.getSettingValueByTag2("vehicleImg_upload"))) {
            if (StringUtils.isEmpty(hyVehicle.getVehicleImg())) {
                throw new JeecgBootException("请上传车辆照片(人和车和车牌)照片!");
            }
        }
//      检查车辆挂靠协议没什么用
//        if (StringUtils.isNotEmpty(hyVehicle.getOwner()) &&
//                hyVehicle.getOwner().contains("公司") || hyVehicle.getOwner().contains("车队")
//        ) {
//            hyVehicle.setIzAffiliation("1");
//        }
//
//        if ("1".equals(hyVehicle.getIzAffiliation())) {
//            if (StringUtils.isEmpty(hyVehicle.getAffiliationAgreement())) {
//                throw new JeecgBootException("请签署车辆挂靠声明！");
//            }
//        }
    }


    /**
     * 比较当前时间 是否在timeArray 范围内
     * @param timezone 时区
     * @param timeArray 开始时间，结束时间
     * @return
     */
    public static boolean isInDate(Date now,String[] timeArray,String pattern,String timezone){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        TimeZone timeZone = TimeZone.getTimeZone(timezone);
        sdf.setTimeZone(timeZone);
        //转换为对应时区时间
        Date date = DateUtils.str2Date(sdf.format(now),sdf);

        Date start = DateUtils.str2Date(timeArray[0],sdf);
        Date end = DateUtils.str2Date(timeArray[1],sdf);

        if (start.after(end) || start.equals(end)) {
            /**
             * 直接用时间判断
             */
            if (date.getTime() >= start.getTime() || date.getTime() <= end.getTime()) {
                return true;
            }

        }else {
            if (date.getTime() >= start.getTime() && date.getTime() <= end.getTime()) {
                return true;
            }
        }
        return false;
    }


    public static boolean matchRegular(String str) {
        String regexIsHanZi = "[\\u4e00-\\u9fa5]+";
        Pattern pattern = Pattern.compile(regexIsHanZi);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }


    /**
     * 通过pid获取车辆类型
     *
     * @param pid
     * @return
     */
//    @AutoLog(value = "APP车辆信息-获取车辆类型")
    @ApiOperation(value = "APP车辆信息-获取车辆类型", httpMethod = "GET", notes = "APP车辆信息-获取车辆类型")
    @RequestMapping(value = "/getvehiclettype", method = {RequestMethod.GET})
    public Result<?> getHyVehicleType(@RequestParam(name = "pid") Integer pid) {
        if (pid == null) {
            return Result.error("pid不能为空！");
        }
        return hyVehicleService.getHyVehicleTypeById(pid);
    }

    //    @AutoLog(value = "APP车辆信息-获取当前车辆信息")
    @ApiOperation(value = "APP车辆信息-获取当前车辆信息", notes = "APP车辆信息-获取当前车辆信息")
    @GetMapping(value = "/getListVehicletInfo")
    public Result<?> getListVehicletInfo(@RequestParam(name = "examineStatus") String examineStatus, HttpServletRequest req) {
        HyDriver driver = userUtil.getDriver();
        if (driver == null) {
            return Result.error("您的司机信息不存在，请补充司机信息！");
        }

        List<String> vehicleIds = hyDriverVehicleService.getVehicleIdListByDriverId(driver.getId());
        if (CollectionUtils.isEmpty(vehicleIds)) {
            return Result.error("您未绑定车辆信息，请先绑定车辆信息！");
        }

        HyVehicle hyVehicle = new HyVehicle();
        QueryWrapper<HyVehicle> queryVehicleWrapper = QueryGenerator.initQueryWrapper(hyVehicle, req.getParameterMap());
        queryVehicleWrapper.in("id", vehicleIds);
        //过滤状态。
        if (StringUtils.isNotEmpty(examineStatus) &&
                ExamineStatusEnum.isValid(examineStatus) &&
                ExamineStatusEnum.getExamineStatusEnumByVal(examineStatus).getCode().equals(examineStatus)) {
            queryVehicleWrapper.eq("examine_status", examineStatus);
        }
        List<?> list = dictUtil.parseArrayListDict(hyVehicleService.list(queryVehicleWrapper));
        return Result.OK(list);
    }

    //    @AutoLog(value = "APP车辆信息-车辆号码获取车辆信息")
    @ApiOperation(value = "APP车辆信息-车辆号码获取车辆信息", httpMethod = "GET", notes = "APP车辆信息-车辆号码获取车辆信息")
    @RequestMapping(value = "/getVehicletInfoByNumber", method = {RequestMethod.GET})
    @EntityDict
    public Result<?> getVehicletInfoByNumber(@RequestParam(name = "VehicleNumber", required = true) String VehicleNumber) {
        return Result.OK(hyVehicleService.getHyVehicleByVehicleNumber(VehicleNumber));
    }


    @AutoLog(value = "APP车辆信息-司机解绑车辆")
    @ApiOperation(value = "APP车辆信息-司机解绑车辆", httpMethod = "POST", notes = "APP车辆信息-司机解绑车辆")
    @PostMapping(value = "/unbundlingVehiclet")
    public Result<?> unbundlingVehiclet(@RequestParam(name = "vehicleId", required = true) String vehicleId) {
        HyDriver driver = userUtil.getDriver();
        if (driver == null) {
            return Result.error("您的司机信息不存在！");
        }
        HyVehicle hyVehicle = hyVehicleService.getById(vehicleId);
        if (hyVehicle == null) {
            return Result.error("您的车辆信息不存在！");
        }
        HyDriverVehicle hyDriverVehicle = hyDriverVehicleService.getInfoByDriverIdAndVehicleId(
                driver.getId(), vehicleId);
        if (hyDriverVehicle == null) {
            return Result.error("解绑失败，您的车辆信息未绑定！");
        }

        hyDriverVehicle.setIzUsed(0);
        hyDriverVehicle.setUpdateTime(new Date());
        if (!hyDriverVehicleService.updateById(hyDriverVehicle)) {
            return Result.error("解绑失败！");
        }

        //调用辽宁税务车辆司机解绑关系接口
        //hyVehicleService.lnTaxUploadUntieVehicleDriver(hyVehicle);

        //如果解绑成功，清除车辆表中的司机ID
        this.hyVehicleService.update(new LambdaUpdateWrapper<HyVehicle>()
                .eq(HyVehicle::getId, vehicleId)
                .set(HyVehicle::getDriverId, StringUtils.EMPTY));

        return Result.OK("解绑成功！");
    }

    public static HyVehicle HyVehicleReqTOHyVehicle(HyVehicleReq hyVehicleReq) {
        if (hyVehicleReq == null) {
            return null;
        }
        HyVehicle hyVehicle = new HyVehicle();
        hyVehicle.setId(hyVehicleReq.getId());
        hyVehicle.setVehicleNumber(hyVehicleReq.getVehicleNumber());
        hyVehicle.setVehiclePlateColorCode(
                StringUtils.isNotEmpty(hyVehicleReq.getVehiclePlateColorCode()) ? hyVehicleReq.getVehiclePlateColorCode() : "2"
        );
        hyVehicle.setVehiclePtype(hyVehicleReq.getVehiclePtype());
        hyVehicle.setVehicleType(hyVehicleReq.getVehicleType());

        String vehicleLong = StringUtils.isNotBlank(hyVehicleReq.getVehicleLong()) ? hyVehicleReq.getVehicleLong() : "7450";
        //int vehicleLongInt = Integer.parseInt(vehicleLong);
        //vehicleLongInt = vehicleLongInt >= 7000 && vehicleLongInt <= 8000 ? vehicleLongInt : 7450;
        //hyVehicle.setVehicleLong(String.valueOf(vehicleLongInt));
        hyVehicle.setVehicleLong(vehicleLong);

        String vehicleWidth = StringUtils.isNotBlank(hyVehicleReq.getVehicleWidth()) ? hyVehicleReq.getVehicleWidth() : "2550";
        //int vehicleWidthInt = Integer.parseInt(vehicleWidth);
        //vehicleWidthInt = vehicleWidthInt >= 2000 && vehicleWidthInt <= 3000 ? vehicleWidthInt : 2550;
        //hyVehicle.setVehicleWidth(String.valueOf(vehicleWidthInt));
        hyVehicle.setVehicleWidth(vehicleWidth);

        String vehicleHeight = StringUtils.isNotBlank(hyVehicleReq.getVehicleHeight()) ? hyVehicleReq.getVehicleHeight() : "3560";
        //int vehicleHeightInt = Integer.parseInt(vehicleHeight);
        //vehicleHeightInt = vehicleHeightInt >= 3000 && vehicleHeightInt <= 4000 ? vehicleHeightInt : 3560;
        //hyVehicle.setVehicleHeight(String.valueOf(vehicleHeightInt));
        hyVehicle.setVehicleHeight(vehicleHeight);

        hyVehicle.setBelongType(hyVehicleReq.getBelongType());
        hyVehicle.setCarrierId(hyVehicleReq.getCarrierId());
        hyVehicle.setDriverId(hyVehicleReq.getDriverId());
        hyVehicle.setDrivingLicenseNum("001".equals(hyVehicleReq.getDrivingLicenseNum()) ? "" : hyVehicleReq.getDrivingLicenseNum());
        hyVehicle.setDrivingLicenseImg(hyVehicleReq.getDrivingLicenseImg());
        hyVehicle.setDrivingLicenseImg2(hyVehicleReq.getDrivingLicenseImg2());
        hyVehicle.setBrandName(hyVehicleReq.getBrandName());
        hyVehicle.setOwner(hyVehicleReq.getOwner());
        hyVehicle.setUseCharacter(
                StringUtils.isNotEmpty( hyVehicleReq.getUseCharacter()) ? hyVehicleReq.getUseCharacter() : "货运"
        );
        hyVehicle.setVin(hyVehicleReq.getVin());
        hyVehicle.setCreditCode(hyVehicleReq.getCreditCode());
        hyVehicle.setIssuingOrganizations(hyVehicleReq.getIssuingOrganizations());
        hyVehicle.setRegisterDate(
                StringUtils.isNotEmpty(hyVehicleReq.getRegisterDate()) ? DateUtils.getStrToDate(hyVehicleReq.getRegisterDate()) : null
        );
        hyVehicle.setIssueDate(
                StringUtils.isNotEmpty(hyVehicleReq.getIssueDate()) ? DateUtils.getStrToDate(hyVehicleReq.getIssueDate()) : null
        );
        hyVehicle.setVehicleEnergyType(hyVehicleReq.getVehicleEnergyType());

        BigDecimal vehicleTonnage = StringUtils.isNotEmpty(hyVehicleReq.getVehicleTonnage()) ? new BigDecimal(hyVehicleReq.getVehicleTonnage()).setScale(2, BigDecimal.ROUND_DOWN) :
                new BigDecimal("40.00");
        BigDecimal a = new BigDecimal("100.00");
        if (a.compareTo(vehicleTonnage) < 0) { //如果核载质量输入的数值大于100，默认按40
            vehicleTonnage = new BigDecimal("40.00");
        }
        hyVehicle.setVehicleTonnage(vehicleTonnage);
        BigDecimal grossMass = StringUtils.isNotEmpty(hyVehicleReq.getGrossMass()) ? new BigDecimal(hyVehicleReq.getGrossMass()).setScale(2, BigDecimal.ROUND_DOWN) :
                new BigDecimal("48.00");
        BigDecimal b = new BigDecimal("100.00");
        if (b.compareTo(grossMass) < 0) { //如果总吨位输入的数值大于100，默认按48
            grossMass = new BigDecimal("48.00");
        }
        hyVehicle.setGrossMass(grossMass);

        hyVehicle.setRoadTransportNumber(hyVehicleReq.getRoadTransportNumber());
        if (StringUtils.isNotEmpty(hyVehicleReq.getRoadTransportImg())) {
            hyVehicle.setRoadTransportImg(hyVehicleReq.getRoadTransportImg());
        }

        if (StringUtils.isNotEmpty(hyVehicleReq.getRoadTransportImg2())) {
            hyVehicle.setRoadTransportImg2(hyVehicleReq.getRoadTransportImg2());
        }

        hyVehicle.setRoadTransportExpiredDate(
                StringUtils.isNotEmpty(hyVehicleReq.getRoadTransportExpiredDate()) ? DateUtils.getStrToDate(hyVehicleReq.getRoadTransportExpiredDate()) : null

        );
        hyVehicle.setBusinessLicense(
                oConvertUtils.getNumberFromString(hyVehicleReq.getBusinessLicense())
        );

        hyVehicle.setCreateBy(hyVehicleReq.getCreateBy());
        hyVehicle.setUpdateBy(hyVehicleReq.getUpdateBy());
        hyVehicle.setVehicleImg(hyVehicleReq.getVehicleImg());
        hyVehicle.setIzAffiliation(hyVehicleReq.getIzAffiliation());
        hyVehicle.setAffiliationAgreement(hyVehicleReq.getAffiliationAgreement());

        hyVehicle.setRegistCertNo(hyVehicleReq.getRegistCertNo());
        hyVehicle.setVehicleAddress(hyVehicleReq.getVehicleAddress());
        hyVehicle.setRoadTransportName(hyVehicleReq.getRoadTransportName());

        hyVehicle.setEmissionStandardType(hyVehicleReq.getEmissionStandardType());
        hyVehicle.setEnvironmentalCertificate(hyVehicleReq.getEnvironmentalCertificate());
        hyVehicle.setExamineStatus(hyVehicleReq.getExamineStatus());
        hyVehicle.setVehicleRegistration(hyVehicleReq.getVehicleRegistration());

        hyVehicle.setTrailerVehiclePlateNumber(hyVehicleReq.getTrailerVehiclePlateNumber());
        hyVehicle.setTrailerVehiclePlateColorCode(hyVehicleReq.getTrailerVehiclePlateColorCode());
        hyVehicle.setTrailerVehiclLicenseImg(hyVehicleReq.getTrailerVehiclLicenseImg());
        hyVehicle.setTrailerVehiclLicenseImg2(hyVehicleReq.getTrailerVehiclLicenseImg2());

        hyVehicle.setTrailerVehicleRoadTransportImg(hyVehicleReq.getTrailerVehicleRoadTransportImg());
        hyVehicle.setTrailerVehicleType(hyVehicleReq.getTrailerVehicleType());
        hyVehicle.setTrailerVehicleRoadTransportNumber(hyVehicleReq.getTrailerVehicleRoadTransportNumber());

        return hyVehicle;
    }


}
